package com.yanqu.road.server.cmd.activity.jiuzhou.cross;

import com.yanqu.road.entity.activity.jiuzhou.config.JiuZhouConfig;
import com.yanqu.road.entity.enums.activity.jiuzhou.eJiuZhouGameZoneType;
import com.yanqu.road.entity.enums.eMailType;
import com.yanqu.road.entity.player.UserInfo;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.jiuzhou.CrossJiuZhouProto;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.Command;
import com.yanqu.road.server.logic.mail.MailManager;
import com.yanqu.road.server.manger.MultipleLanguageMgr;
import com.yanqu.road.server.manger.activity.jiuzhou.JiuZhouMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;
import io.netty.channel.ChannelHandlerContext;

import java.text.MessageFormat;

@Cmd(code = GameProtocol.S_JIU_ZHOU_SUPPORT_MAIL_REWARD_FROM_CROSS, desc = "九州邮件奖励")
public class JiuZhouSupportMailRewardFromCrossCmd implements Command {


    @Override
    public void execute(ChannelHandlerContext ctx, YanQuMessage packet) throws Exception {

        JiuZhouConfig config = JiuZhouMgr.getConfig();
        if (config == null) {
            return;
        }

        CrossJiuZhouProto.CrossJiuZhouSupportMailRewardMsg reqMsg = CrossJiuZhouProto.CrossJiuZhouSupportMailRewardMsg.parseFrom(packet.getBytes());
        long userId = reqMsg.getSupportUserId();

        UserInfo userInfo = UserMgr.getUserInfo(userId);
        if (userInfo == null) {
            return;
        }
        // 哪个赛区
        int zoneType = reqMsg.getZoneType();

        // 谁
        String targetNickName = reqMsg.getTargetNickName();
        // 第几名
        int rankNum = reqMsg.getRankNum();

        // 成功了吗
        boolean success = reqMsg.getSuccess();

        // 声援了多少次
        int supportCnt = reqMsg.getSupportCnt();

        // 什么奖励
        Property supportReward = new Property();
        if (success && reqMsg.hasSupportReward()) {
            supportReward = PropertyHelper.parseStringToProperty(reqMsg.getSupportReward());
            supportReward.goodCountMultiNum(supportCnt);
        }



        String language = userInfo.getLanguage();

        // 赛场名称
        String zoneName = "";
        if (zoneType == eJiuZhouGameZoneType.elite.getValue()) {
            zoneName = MultipleLanguageMgr.getContent("NINECHESS_ZONE_KEY_1", language);
        } else if (zoneType == eJiuZhouGameZoneType.master.getValue()) {
            zoneName = MultipleLanguageMgr.getContent("NINECHESS_ZONE_KEY_2", language);
        } else if (zoneType == eJiuZhouGameZoneType.king.getValue()) {
            zoneName = MultipleLanguageMgr.getContent("NINECHESS_ZONE_KEY_3", language);
        }

        String mailTitle = MultipleLanguageMgr.getContent(MailManager.NINECHESS_SUPPORT_TITLE, language);
        String mailContent = "";
        if (success) {
            mailContent = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.NINECHESS_SUPPORT_CONTENT_1, userInfo.getLanguage())
                    , zoneName, targetNickName, rankNum, supportCnt);
        } else {
            mailContent = StringUtils.format(MultipleLanguageMgr.getContent(MailManager.NINECHESS_SUPPORT_CONTENT_2, userInfo.getLanguage())
                    , zoneName, targetNickName, config.getSupportRankNum());
        }

        MailManager.sendMail(userId, eMailType.DAILY.getValue(), PropertyHelper.parsePropertyToString(supportReward), mailContent, mailTitle, config.getActivityInfo().getActivityId(), config.getActivityInfo().getServerListStr());
    }

}
